/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"9683B1C8-B316-478C-9287-1FAF2A367BAC",variableType:4}
 */
var fCodSub = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"6F33A243-B470-4D81-9CB4-9CB934F56389"}
 */
var fDescSub = null;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"BFB5C42D-E870-4F42-990C-641457A72A07"}
 */
function onShow_alta(firstShow, event) {
	globals.gvarios = 5
	globals.gcapitulo = capitulo
	globals.gsubcapitulo = subcapitulo
	elements.bot_nueva.visible = marca_sub
	elements.alta_scuenta.visible = false
	databaseManager.setAutoSave(false)
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"7B30D022-F2B1-45E4-9773-2446C177E17C"}
 */
function onAction_alta(event) {
	var estado = databaseManager.saveData()
	if (estado == false)
	{
		globals.DIALOGS.showWarningDialog('Aviso',"Error al actualizar los datos","Ok")
		return
	}
	var	$win = application.getWindow("CUENTAS")
	$win.hide()
	$win.destroy()
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"63DF3862-54C7-4378-B198-19882A4FA3F0"}
 */
function onAction_salir(event) {
	databaseManager.revertEditedRecords()
	databaseManager.setAutoSave(true)
	var	$win = application.getWindow("CUENTAS")
	$win.hide()
	$win.destroy()
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"EC9818D0-047C-40AF-BC8D-0595BEBE2893"}
 */
function onAction_nueva_subcta(event) {
	elements.alta_scuenta.visible = true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"A0133615-05F4-4762-BE24-0795DA9834AB"}
 * @AllowToRunInFind
 */
function onAction_altaScuenta(event) {
	if (fCodSub == null || fDescSub == null || fDescSub.length > 40)
	{
		globals.DIALOGS.showWarningDialog('Aviso','Datos incorrectos','OK')
	}
	else
	{
		var estado = cuentas_to_subcuentas.find()
		if (estado == false)
		{
			globals.DIALOGS.showWarningDialog('Aviso','Error al hacer el find','OK')
		}
		else
		{
			cuentas_to_subcuentas.subcuenta = fCodSub
			var cuantos = cuentas_to_subcuentas.search()
			if (cuantos != 0)
			{
				cuentas_to_subcuentas.loadAllRecords()
				elements.alta_scuenta.visible = false
				globals.DIALOGS.showWarningDialog('Aviso','La subcuenta ya existe','OK')			
			}
			else
			{
				cuentas_to_subcuentas.newRecord()
				cuentas_to_subcuentas.subcuenta = fCodSub
				cuentas_to_subcuentas.descripcion = fDescSub
				cuentas_to_subcuentas.imputable = 1
				cuentas_to_subcuentas.marca_mon = marca_mon
				cuentas_to_subcuentas.marca_sub = 0
				cuentas_to_subcuentas.activa = 0
				cuentas_to_subcuentas.sinonimo = ' '
				elements.alta_scuenta.visible = false
				fCodSub = null 
				fDescSub = null
				databaseManager.saveData()
				cuentas_to_subcuentas.loadAllRecords()
			}				
		}
	}
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {String} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"EDE1E9F0-AEFD-4285-A5C3-D4E33F601397"}
 */
function onDataChange(oldValue, newValue, event) {
	if (newValue.length > 40)
	{
		globals.DIALOGS.showWarningDialog('Aviso','La descripción tiene más de 40 caracteres','OK')
		return false
	}
	
	return true
}

/**
 * Handle record selected.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"A8B2E080-2708-4146-80B6-DBC5F5324ACB"}
 */
function onRecordSelection(event) {
	elements.bot_nueva.visible = marca_sub
}

/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"D9FF47A5-018E-4BF9-B2D6-B46E3A604B57"}
 */
function onDataChangeMarcaSub(oldValue, newValue, event) {
	if (newValue == 0 && cuentas_to_subcuentas.getSize() != 0)
	{
		globals.DIALOGS.showWarningDialog('Aviso','La cuenta posee subcuentas','OK')
		return false
	}
	imputable = !newValue
	return true
}
